package com.anuprayog.treasureloot;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.content.ContextWrapper;
import java.io.File;

/**
 * @author amit
 * This activity displays the highest score achieved by the user
 */
public class HighActivity extends Activity {

		private ImageView imageview2, imageview3, imageview4, imageview5, imageview6, imageview7;
		
		public int HIGH_SCORE;
		
		private String scoreString;
		
		 private String filename = "MyFile1.txt";
		 private String filepath = "MyFileStorage1";
		 File myInternalFile1;

		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_high);
			
			setVolumeControlStream(AudioManager.STREAM_MUSIC);
			
			//code added by manoj chand on 23 july 2013
			ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
			File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
			myInternalFile1 = new File(directory , filename);
			HIGH_SCORE = getHighScore();
			
			scoreString = String.valueOf(HIGH_SCORE);
			
			imageview2 = (ImageView) findViewById(R.id.imageView2);
			imageview3 = (ImageView) findViewById(R.id.imageView3);
			imageview4 = (ImageView) findViewById(R.id.imageView4);
			imageview5 = (ImageView) findViewById(R.id.imageView5);
			imageview6 = (ImageView) findViewById(R.id.imageView6);
			imageview7 = (ImageView) findViewById(R.id.imageView7);
			
			if (scoreString.charAt(0) == '0'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.zero));
			} else if (scoreString.charAt(0) == '1'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.one));
			} else if (scoreString.charAt(0) == '2'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.two));
			} else if (scoreString.charAt(0) == '3'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.three));
			} else if (scoreString.charAt(0) == '4'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.four));
			} else if (scoreString.charAt(0) == '5'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.five));
			} else if (scoreString.charAt(0) == '6'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.six));
			} else if (scoreString.charAt(0) == '7'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seven));
			} else if (scoreString.charAt(0) == '8'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.eight));
			} else if (scoreString.charAt(0) == '9'){
				imageview2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.nine));
			} 
			
			if (HIGH_SCORE>9){
				if (scoreString.charAt(1) == '0'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.zero));
				} else if (scoreString.charAt(1) == '1'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.one));
				} else if (scoreString.charAt(1) == '2'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.two));
				} else if (scoreString.charAt(1) == '3'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.three));
				} else if (scoreString.charAt(1) == '4'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.four));
				} else if (scoreString.charAt(1) == '5'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.five));
				} else if (scoreString.charAt(1) == '6'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.six));
				} else if (scoreString.charAt(1) == '7'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seven));
				} else if (scoreString.charAt(1) == '8'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.eight));
				} else if (scoreString.charAt(1) == '9'){
					imageview3.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.nine));
				} 
			}
			
			if (HIGH_SCORE>99){
				if (scoreString.charAt(2) == '0'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.zero));
				} else if (scoreString.charAt(2) == '1'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.one));
				} else if (scoreString.charAt(2) == '2'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.two));
				} else if (scoreString.charAt(2) == '3'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.three));
				} else if (scoreString.charAt(2) == '4'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.four));
				} else if (scoreString.charAt(2) == '5'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.five));
				} else if (scoreString.charAt(2) == '6'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.six));
				} else if (scoreString.charAt(2) == '7'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seven));
				} else if (scoreString.charAt(2) == '8'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.eight));
				} else if (scoreString.charAt(2) == '9'){
					imageview4.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.nine));
				} 
			}
			
			if (HIGH_SCORE>999){
				if (scoreString.charAt(3) == '0'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.zero));
				} else if (scoreString.charAt(3) == '1'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.one));
				} else if (scoreString.charAt(3) == '2'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.two));
				} else if (scoreString.charAt(3) == '3'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.three));
				} else if (scoreString.charAt(3) == '4'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.four));
				} else if (scoreString.charAt(3) == '5'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.five));
				} else if (scoreString.charAt(3) == '6'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.six));
				} else if (scoreString.charAt(3) == '7'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seven));
				} else if (scoreString.charAt(3) == '8'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.eight));
				} else if (scoreString.charAt(3) == '9'){
					imageview5.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.nine));
				} 
			}
			
			if (HIGH_SCORE>9999){
				if (scoreString.charAt(4) == '0'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.zero));
				} else if (scoreString.charAt(4) == '1'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.one));
				} else if (scoreString.charAt(4) == '2'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.two));
				} else if (scoreString.charAt(4) == '3'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.three));
				} else if (scoreString.charAt(4) == '4'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.four));
				} else if (scoreString.charAt(4) == '5'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.five));
				} else if (scoreString.charAt(4) == '6'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.six));
				} else if (scoreString.charAt(4) == '7'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seven));
				} else if (scoreString.charAt(4) == '8'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.eight));
				} else if (scoreString.charAt(4) == '9'){
					imageview6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.nine));
				} 
			}
			
			if (HIGH_SCORE>99999){
				if (scoreString.charAt(5) == '0'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.zero));
				} else if (scoreString.charAt(5) == '1'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.one));
				} else if (scoreString.charAt(5) == '2'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.two));
				} else if (scoreString.charAt(5) == '3'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.three));
				} else if (scoreString.charAt(5) == '4'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.four));
				} else if (scoreString.charAt(5) == '5'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.five));
				} else if (scoreString.charAt(5) == '6'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.six));
				} else if (scoreString.charAt(5) == '7'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seven));
				} else if (scoreString.charAt(5) == '8'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.eight));
				} else if (scoreString.charAt(5) == '9'){
					imageview7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.nine));
				} 
			}
			
		}
		
		//function created by manoj chand to get high score from internal storage.
		public int getHighScore(){
			String myData="0";
			   try {
				    FileInputStream fis = new FileInputStream(myInternalFile1);
				    DataInputStream in = new DataInputStream(fis);
				    BufferedReader br = new BufferedReader(new InputStreamReader(in));
				    String strLine;
				    while ((strLine = br.readLine()) != null) {
				     myData = strLine;
				    }
				    in.close();
				   } catch (IOException e) {
				    e.printStackTrace();
				   }
			   if(myData != null && myData.trim().equals(""))
				   myData="0";
			   return Integer.parseInt(myData);
			}

		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// Inflate the menu; this adds items to the action bar if it is present.
			getMenuInflater().inflate(R.menu.high, menu);
			return true;
		}
		
		public void goBack(View view) {
			finish();
	    }

	}